package com.dfrz.dao;

import com.dfrz.entry.BigType;
import com.dfrz.util.JdbcUtils;
import com.mysql.cj.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BigTypeDao {
    public int insertBigtype(BigType bigType){
        int count=0;
        String sqlStr="insert into bigtype(name,remarks)";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,bigType.getName(),bigType.getRemarks());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;

    }
    public int deletBigType(Integer id){
        int count=0;
        String sqlStr="update bigtype set status=-1 where id=?";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    public int updateBigType(BigType bigType){
        int count=0;
        String sqlStr="update bigtype set name=?,remarks=?";
        QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
        try {
            count=queryRunner.update(sqlStr,bigType.getName(),bigType.getRemarks());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
    public List<BigType> queryBigType(String name,int limit,int startNum){
        List<BigType>listBig=null;
        String sqlStr=null;
        if (!StringUtils.isNullOrEmpty(name)){
            sqlStr="select * from bigtype where name like '?' and status=1 order by id desc limit?,?";
            QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
            try {
                listBig=queryRunner.query(sqlStr,new BeanListHandler<BigType>(BigType.class),name,startNum,limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }else {
            sqlStr="select * from bigtype where status=1 limit ?,?";
            QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
            try {
                listBig=queryRunner.query(sqlStr,new BeanListHandler<BigType>(BigType.class),startNum,limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return listBig;
    }
    public int queryCount(String name){
        long countL=0;
        String sqlStr=null;
        if (!StringUtils.isNullOrEmpty(name)){
            sqlStr="select COUNT(1) from bigtype where name like '?' and status=1 order by id desc limit?,?";
            QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
            try {
                countL=queryRunner.query(sqlStr,new ScalarHandler<>(),name);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }else {
            sqlStr="select COUNT(1) from bigtype where status=1 limit ?,?";
            QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
            try {
                countL=queryRunner.query(sqlStr,new ScalarHandler<>());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return (int) countL;
    }

}
